package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.dao.BpmNodeMessageDao;
import com.ccp.dev.workflow.model.BpmNodeMessage;
import com.ccp.dev.workflow.model.BpmNodeSet;
import com.ccp.dev.workflow.model.BpmUserCondition;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassDescribe:
 * 流程节点消息
 * @author :wangcheng
 * Date: 2019-07-17
 */
@Service
public class BpmNodeMessageService extends BaseService<BpmNodeMessage> {
    @Resource
    private BpmNodeMessageDao bpmNodeMessageDao;
    @Resource
    private BpmNodeSetService bpmNodeSetService;
    @Resource
    private BpmUserConditionService bpmUserConditionService;
    @Resource
    private BpmNodeUserService bpmNodeUserService;

    /**
     * 获取节点消息
     * @param procDefId 流程定义id
     * @param nodeId 节点id
     * @return 信息列表
     */
    public List<BpmNodeMessage> getNodeMessage(String procDefId, String nodeId) {
       return bpmNodeMessageDao.queryByProcDefIdAndNodeId(procDefId, nodeId);
    }

    /**
     * 获取信息接收人
     * @param execution 流程实例
     * @param preTaskUserId 前一个任务执行人
     * @return 接收人列表
     */
    @Transactional(rollbackFor = Exception.class)
    public List<SysUser> getInnerReceiver(DelegateExecution execution, String preTaskUserId){
        return getMessageReceiver(execution, BpmUserCondition.CONDITION_TYPE_MSG_INNER_RECEIVER, preTaskUserId);
    }

    /**
     * 获取节点消息接收人
     * @param execution 流程实例
     * @param type 发送类型
     * @param preTaskUserId 上一个用户id
     * @return 接收人列表
     */
    private List<SysUser> getMessageReceiver(DelegateExecution execution, Integer type, String preTaskUserId) {
        String procDefId = execution.getProcessDefinitionId();
        String nodeId = execution.getCurrentActivityId();
        BpmNodeSet nodeSet = bpmNodeSetService.getBySetType(procDefId, BpmNodeSet.SETTYPE_GLOABLEFORM);
        if (nodeSet == null) {
            return Collections.emptyList();
        }
        List<BpmUserCondition> bpmUserConditions;
        if(type==null){
            type=-1;
        }

        switch (type) {
            case BpmUserCondition.CONDITION_TYPE_MSG_INNER_RECEIVER:
                bpmUserConditions = bpmUserConditionService.getReceiverInnerConditions(procDefId, nodeId);
                break;
            default:
                bpmUserConditions = new ArrayList<>();
                break;
        }

        return bpmNodeUserService.getTaskExecutors(execution, bpmUserConditions, preTaskUserId);
    }

    /**
     * 通过流程定义ID及节点id获取流程设置节点列表
     * @param procDefId 流程定义id
     * @param nodeId 节点id
     * @return 节点信息列表
     */
    public List<BpmNodeMessage> queryByProcDefIdAndNodeId(String procDefId, String nodeId) {
        return bpmNodeMessageDao.queryByProcDefIdAndNodeId(procDefId, nodeId);
    }

}
